package com.scoy.honeymei.fragment.home;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.HttpParams;
import com.scoy.honeymei.R;
import com.scoy.honeymei.activity.home.FilmDetailActivity;
import com.scoy.honeymei.activity.mall.SearchHomeActivity;
import com.scoy.honeymei.adapter.FilmAdapter;
import com.scoy.honeymei.base.BaseFragment;
import com.scoy.honeymei.bean.FilmBean;
import com.scoy.honeymei.bean.FilmListBean;
import com.scoy.honeymei.http.HpGo;
import com.scoy.honeymei.url.ApiCallBack;
import com.scoy.honeymei.url.MyUrl;
import com.scoy.honeymei.utils.MyUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @ClassName: FilmFragment$
 * @Description:
 * @Author: GaoZhan
 * @CreateDate: 2019/9/4/0004$ 上午 9:34$
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/9/4/0004$ 上午 9:34$
 * @UpdateRemark: 更新内容
 */
public class FilmFragment extends BaseFragment {
    @BindView(R.id.normal_rv)
    RecyclerView normalRv;
    @BindView(R.id.nodata_tv)
    TextView nodataTv;
    @BindView(R.id.nor_srl)
    SmartRefreshLayout norSrl;
    private int type;
    private Unbinder unbinder;
    private Context mContext;
    private ArrayList<FilmBean> datalist;
    private FilmAdapter filmAdapter;
    private long cTime;

    private int pageInt;
    private int searchId;
    private String searchName;
    private int mid;
    private int cinemaId;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            type = getArguments().getInt("type");
            cTime = getArguments().getLong("cTime");
            mid = getArguments().getInt("mid");
            searchId = getArguments().getInt("searchId");
            searchName = getArguments().getString("searchName");
            cinemaId = getArguments().getInt("cinemaId");
        }
    }

    public static FilmFragment newInstance(int type, long cTime, int mid, int cinemaId) {
        Bundle args = new Bundle();
        args.putInt("type", type);
        args.putLong("cTime", cTime);
        args.putInt("mid", mid);
        args.putInt("cinemaId", cinemaId);
        FilmFragment fragment = new FilmFragment();
        fragment.setArguments(args);
        return fragment;
    }

    public static FilmFragment newInstance(int searchId/*, String searchName*/) {
        Bundle args = new Bundle();
        args.putInt("searchId", searchId);
        // args.putString("searchName", searchName);
        FilmFragment fragment = new FilmFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.custom_fragment_list, null, false);
        unbinder = ButterKnife.bind(this, view);
        mContext = getContext();


        initNormal();

        if (searchId == 1) {//首页搜索
            httpFilmSearch(0);
        } else {
            httpFilmlist(0);
        }
        return view;
    }

    @Override
    public void initNormal() {
        datalist = new ArrayList<>();

        initRV();

        norSrl.setOnRefreshListener(refreshLayout -> {
            refreshLayout.finishRefresh(1500);
            datalist.clear();
            if (searchId == 1) {
                httpFilmSearch(0);
            } else {
                httpFilmlist(0);
            }


        }).setOnLoadMoreListener(refreshLayout -> {
            refreshLayout.finishLoadMore(1500);
            if (searchId == 1) {
                httpFilmSearch(pageInt);
            } else {
                if (pageInt == -1) {
                    MyUtil.mytoast(mContext, getString(R.string.nomore));
                } else {
                    httpFilmlist(pageInt);
                }
            }

        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    private void initRV() {
        filmAdapter = new FilmAdapter(mContext, datalist, nodataTv);
        normalRv.setLayoutManager(new LinearLayoutManager(mContext, RecyclerView.VERTICAL, false));
        normalRv.setAdapter(filmAdapter);
        filmAdapter.setOnOneClick(pos -> {
            FilmBean movieBean = datalist.get(pos);
            Intent intentde = new Intent(mContext, FilmDetailActivity.class);
            intentde.putExtra("fId", movieBean.getId());
            intentde.putExtra("cinemaId", cinemaId);
            startActivity(intentde);
        });

    }

    private void httpFilmlist(int myPage) {
        HttpParams params = new HttpParams();
        params.put("date", cTime);
        params.put("page", myPage);
        params.put("mid", mid);
        params.put("cinema_id", cinemaId);
        HpGo.newInstance().HttpGo(getActivity(), MyUrl.FILM_TIMESHOW_LIST, params, new ApiCallBack() {
            @Override
            public void onSuccess(String o) {
                FilmListBean filmListBean = new Gson().fromJson(o, FilmListBean.class);
                int next = filmListBean.getNext();
                List<FilmBean> movie = filmListBean.getMovie();
                pageInt = myPage + 1;
                if (next == 0) {
                    pageInt = -1;
                }
                datalist.addAll(movie);
                filmAdapter.notifyDataSetChanged();
            }
        });

    }

    private void httpFilmSearch(int myPage) {
        HttpParams params = new HttpParams();
        params.put("type", searchId);
        params.put("search_name", ((SearchHomeActivity) getActivity()).getSearchStr());
        params.put("page", myPage);
        HpGo.newInstance().HttpGo(getActivity(), MyUrl.HOME_SEARCH, params, new ApiCallBack() {
            @Override
            public void onSuccess(String o) {
                List<FilmBean> movie = new Gson().fromJson(o, new TypeToken<List<FilmBean>>() {
                }.getType());
                pageInt = myPage + 1;
                datalist.addAll(movie);
                filmAdapter.notifyDataSetChanged();
            }
        });

    }


}
